// import ol from 'openlayers';
import { transformExtent } from 'ol/proj'
import { getTransform } from 'ol/proj'
import MousePosition from 'ol/control/MousePosition'
import { createStringXY } from 'ol/coordinate'
import Graticule from 'ol/layer/Graticule'
import style from './style'
import { config } from './config'
export default {
    // 获取层级
    getZoom(map) {
        return map.getView().getZoom()
    },
    // 放大
    zoomIn(map, zoom = 0.3) {
        this.setZoom(map, zoom)
    },
    // 缩小
    zoomOut(map, zoom = 0.3) {
        console.log('out', zoom)
        this.setZoom(map, -zoom)
    },
    // 设置任意
    setZoom(map, num) {
        console.log(num)
        const view = map.getView()
        view.setZoom(view.getZoom() + num)
    },
    // 设置层级，有动画的
    setCurZoom(map, zoom) {
        const view = map.getView()
        view.animate({
            duration: 1000,
            zoom: parseFloat(zoom) || 8
        })
    },
    // 获取中心点
    getCenter(map) {
        return map.getView().getCenter()
    },
    // 设置中心点
    setCenter(map, lonlat, zoom) {
        map.getView().setCenter(this.transform(lonlat))
        if (zoom) {
            map.getView().setZoom(zoom)
        }
    },
    // 移动到某个点，动画形式
    panTo(map, lonlat, zoom) {
        let view = map.getView()
        view.animate({
            duration: 1000,
            center: this.transform(lonlat),
            zoom: zoom || 8
        })
    },
    // 获取View视图对象
    getView(map) {
        return map.getView()
    },
    // 获取地图范围
    getExtent(map, reverse = false) {
        let extent = map.getView().calculateExtent(map.getSize())
        if (reverse) {
            extent = transformExtent(extent, 'EPSG:4326', 'EPSG:3857')
        }
        return extent
    },
    // 获取经事件的纬度
    getLonLat(e) {
        return this.transform(e.coordinate, true)
    },
    // 移动地图位置和设置层级
    flyTo(map, lonlat, zoom) {
        map.getView().animate({
            center: this.transform(lonlat),
            duration: 1000,
            zoom: zoom || 8
        })
    },
    // 转化坐标格式
    transform(lonlat, reverse = false) {
        const systemProj = reverse ? 'EPSG:4326' : 'EPSG:3857'
        const sourceProj = reverse ? 'EPSG:3857' : 'EPSG:4326'
        return getTransform(sourceProj, systemProj)(this.floatLonLat(lonlat))
    },
    floatLonLat(lonlat) {
        console.log('lonlat', lonlat)
        const result = [...lonlat]
        return result.map((coordinates) => {
            return Number.parseFloat((coordinates + '').trim())
        })
    },
    transforms(lonlats, reverse = false) {
        const result = [...lonlats]
        return result.map((coordinates) => {
            return this.transform(coordinates, reverse)
        })
    },
    transformExtent(lonlat, reverse = false) {
        const systemProj = reverse ? 'EPSG:4326' : 'EPSG:3857'
        const sourceProj = reverse ? 'EPSG:3857' : 'EPSG:4326'
        return transformExtent(lonlat, sourceProj, systemProj)
    },

    /**
     * 获取静态图片所需的地图要素
     * @param {object} params 合并的参数对象
     * @param {object} map 地图对象
     * @return 合并后的参数
     */
    getImageParam(params = {}, map) {
        if (Object.keys(map) === 0) return params
        const bbox = this.getExtent(map)
        const BBOX = bbox ? bbox.join(',') : ''
        const [WIDTH, HEIGHT] = map.getSize()
        const ZOOM = Math.round(map.getView().getZoom())
        return Object.assign({ BBOX, WIDTH, HEIGHT, ZOOM }, params)
    },

    getIndex(type = 'basic') {
        const indexData = {
            basic: 1,
            model: 2,
            impact: 3,
            draw: 4,
            drawImpact: 5,
            poi: 5,
            poitop: 6,
            point: 7,
            active: 8
        }
        return indexData[type]
    },
    // 显示鼠标位置
    showMousePosition(id) {
        // 实例化鼠标位置控件
        return new MousePosition({
            coordinateFormat: createStringXY(2), //小数位数
            projection: 'EPSG:4326', //地图投影坐标系
            className: 'custom-mouse-position', // 自定义类名
            target: document.getElementById(id), // 容器
            undefinedHTML: '--' //未定义坐标的标记
        })
    },
    // 获取鼠标所在的像素
    mousePageXY(e) {
        console.log(e)
        if (e['layerX']) {
            return [e.layerX, e.layerY]
        } else {
            return e.pixel_
        }
    },
    // 获取鼠标所在的经纬度
    mousePageLonlat(e) {
        return e.coordinate
    },
    // 地图网格线组件
    addGraticule(map) {
        let stroke = style.stroke({
            color: 'rgba(192, 190, 190, 1)',
            width: 1
        })
        let graticule = new Graticule({
            targetSize: 150,
            projection: config.sourceProj,
            strokeStyle: stroke
        })
        graticule.setMap(map)
        return graticule
    },
    // 移除网格
    removeGraticule(grat) {
        grat.setMap(null)
    }
}
// # sourceMappingURL=utils.js.map
